/*
 * button_bsp.c
 *
 *  Created on: Dec 13, 2021
 *      Author: justj
 */

#include "ch32v10x.h"
#include <rtthread.h>
#include <rthw.h>
#include "drivers/pin.h"
#include "multi_button.h"

#define CTL_BTN_PIN     16      //PA2

Button ctl_button;
UINT8 ctl_cmd = 0;

UINT8 ctl_btn_in(void)
{
    return rt_pin_read(CTL_BTN_PIN);
}

void ctl_btn_single_click_cb(void *p)
{
    ctl_cmd = 1;
}

void ctl_btn_double_click_cb(void *p)
{
    ctl_cmd = 2;
}

void ctl_btn_long_press_cb(void *p)
{
    ctl_cmd = 3;
}

void btn_init(void)
{
    rt_pin_mode(CTL_BTN_PIN,PIN_MODE_INPUT_PULLUP);

    button_init(&ctl_button, ctl_btn_in, PIN_LOW);
    button_attach(&ctl_button, SINGLE_CLICK, ctl_btn_single_click_cb);
    button_attach(&ctl_button, DOUBLE_CLICK, ctl_btn_double_click_cb);
    button_attach(&ctl_button, LONG_RRESS_START, ctl_btn_long_press_cb);
    button_start(&ctl_button);
}
INIT_COMPONENT_EXPORT(btn_init);


void btn_loop(void)
{
    button_ticks();
}

UINT8 btn_get_status(void)
{
    UINT8 status = ctl_cmd;
    ctl_cmd = 0;
    return status;
}
